<?php

/*
 * This file will perform the ajax for acknowledging a sent attachment that was received by an external receiver.
 * The following tables will be inserted and/or updated with data :
 * Attachment
 * Sent_Attachment
 * Log
 * 
 * Created by: Peter Agno Jr.
 * Date created: Octber 20, 2011
 * 
 * In parameters: attachmentId
 * Out parameters: ackSentFlag
 */

session_start();

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {   
    include('../includes/siteConfig.php'); 

    // connect to database
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');

    $attachmentId = $_POST['attachmentId'];
    
    // Get external account name
    $query =
        "   
            SELECT EA.exAccName as exAccName
            FROM Sent_Attachment as SA, ExternalAccount as EA
            WHERE SA.documentId = '$attachmentId' AND SA.receiveFlag = 'Pending' AND SA.orgFlag = 'External'
                AND SA.receiverId = EA.exAccId
        ";
    
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    $row = mysql_fetch_array($result);
    $extRcvrName = $row['exAccName'];
    
    // Query below will not anymore happen, since externalLoc is removed in Attachment
    // Update Attachment table, location will be NULL, and externalLoc will be the ExternalAccount.exAccId
//    $query =
//        "
//            UPDATE Attachment
//            SET location = null, externalLoc = 
//                (
//                    SELECT exAccId
//                    FROM Sent_Attachment as SA, ExternalAccount as EA
//                    WHERE SA.documentId = '$attachmentId' AND SA.receiveFlag = 'Pending' AND SA.receiverId = EA.exAccName
//                )
//            WHERE attachmentId = '$attachmentId'
//        ";
    
    // Since externalLoc is removed in Attachment, do the query below
    $query = 
        "
            UPDATE Attachment
            SET location = null
            WHERE attachmentId = '$attachmentId'
        ";
    
    mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    
    // Update the Sent_Attachment table, receiveFlag will changed to Received
    $query =
        "
            UPDATE Sent_Attachment
            SET receiveFlag = 'Received'
            WHERE documentId = '$attachmentId' AND receiveFlag = 'Pending'
        ";
    
    mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    
    // Get userId which will be the sender
    $userId = $_SESSION['loggedUserId'];

    // Get current date and time
    date_default_timezone_set('Asia/Singapore');
    $currentDateTime= date("Y-m-d H:i:s" , time());
    
    // Insert data in Log
    $query =
        "
            INSERT INTO Log (logId, documentId, userId, stepId, logDateTime, activity) VALUES
                ('', '$attachmentId', '$extRcvrName', NULL, '$currentDateTime', 'Received');
        ";

    mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    
    $feed['ackSentFlag'] = "Success";
    $feed['msg'] = $attachmentId." was successfully received by ".$extRcvrName.".";
    
    echo json_encode($feed);
    mysql_free_result($result);
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
 echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
